package com.zja.www.dao.impl;

import com.zja.www.dao.AdminDao;
import com.zja.www.entity.Admin;
import com.zja.www.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    private final Connection connection;

    public AdminDaoImpl() {
        this.connection = DBUtil.getConnection();
        if (this.connection == null) {
            throw new RuntimeException("无法获取数据库连接");
        }

    }


    @Override
    public boolean login(String username, String password) {
        String sql = "select count(*)from admin where username=? and password=?";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            return rs.next() && rs.getInt(1) > 0;
        }catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean insert(Admin admin) {
        String sql = "insert into admin(user_name, pass_word, phone) values(?,?,?)";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setString(1, admin.getUserName());
            ps.setString(2, admin.getPassword());
            ps.setString(3, admin.getPhone());

            return ps.executeUpdate() > 0;
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Admin getAdminById(int id) {
        String sql = "select * from admin where id=?";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                Admin admin = new Admin();
                admin.setId(id);
                admin.setUserName(rs.getString("user_name"));
                admin.setPassword(rs.getString("pass_word"));
                admin.setPhone(rs.getString("phone"));
                return admin;
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Admin getAdminByUsername(String username) {
        String sql = "select * from admin where username=?";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                Admin admin = new Admin();
                admin.setId(rs.getInt("id"));
                admin.setUserName(rs.getString("user_name"));
                admin.setPassword(rs.getString("pass_word"));
                admin.setPhone(rs.getString("phone"));
                return admin;
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Admin> getAllAdmins() {
        List<Admin> admins = new ArrayList<>();
        String sql = "select * from admin";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                Admin admin = new Admin();
                admin.setId(rs.getInt("id"));
                admin.setUserName(rs.getString("user_name"));
                admin.setPassword(rs.getString("pass_word"));
                admin.setPhone(rs.getString("phone"));
                admins.add(admin);
            }
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return admins;
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        String sql = "update admin set user_name=?,pass_word=?,phone=? where id=?";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setString(1, admin.getUserName());
            ps.setString(2, admin.getPassword());
            ps.setString(3, admin.getPhone());
            ps.setInt(4, admin.getId());
            return ps.executeUpdate() > 0;

        }catch(SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteAdmin(int id) {
        String sql = "delete from admin where id=?";
        try(PreparedStatement ps = connection.prepareStatement(sql)){
            ps.setInt(1, id);
            return ps.executeUpdate() > 0;
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
